
#include "Group.h"
#include "FLOAT.h"

Group::Group()
{
}

Group::~Group()
{
	std::vector<Triangle *>::iterator itr = m_vpTriangles.begin();
	while (itr!=m_vpTriangles.end())
	{
		delete (Triangle *)&(*itr);
		itr++;
	}
}

void Group::addTriangle(Triangle *pTriangle)
{
	m_vpTriangles.push_back(pTriangle);
}

bool Group::intersect(const Ray *pRay, Hit *pHit, float tmin, float tmax)
{
	Hit tHit,minHit(FLT_MAX, NULL, Vec3f());
	size_t i;
	for (i=0; i < getNumofTriangles(); i++)
	{
		if ( m_vpTriangles[i]->intersect(pRay, &tHit, tmin, tmax) )
			if ( tHit.getT() < minHit.getT() )
			{
				minHit = tHit;
			}
	}
	if ( minHit.getT() < tmax )
	{
		*pHit = minHit;
		return true;
	}
	return false;
}

bool Group::intersectShadowRay(const Ray *pRay, float tmin, float tmax)
{
	size_t i;
	for (i=0; i < getNumofTriangles(); i++)
	{
		if ( m_vpTriangles[i]->intersectShadowRay(pRay, tmin, tmax) )
			return true;
	}
	return false;
}

void Group::debugMessage()
{
	printf("Group{\nTrianglesNumber=%d\n",getNumofTriangles());
	for (size_t i=0;i<getNumofTriangles();i++)
		m_vpTriangles[i]->debugMessage();
	printf("} // Group\n");
}